//
//  IMPeopleViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/5/30.
//

#import "IMPeopleViewController.h"
#import "IMPeopleTopView.h"
#import "ReportViewController.h"
@interface IMPeopleViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) IMPeopleTopView * topView;
@end

@implementation IMPeopleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = self.model.name;
}
- (void)SQ_UIViewForSelfView{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = self.topView;
    [self.topView.avatarImageView.headerImage sd_setImageWithURL:SQURLImage(self.model.portrait)];
    self.topView.nameLabel.text = self.model.name;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    NSArray * array = @[@"举报",@"拉黑",@"清空聊天记录"];
    [self.jsonArray addObjectsFromArray:array];
    [self.tableView reloadData];
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"UITableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text =  [self.jsonArray objectAtIndex:indexPath.item];
    cell.textLabel.textColor = UIColor.blackColor;
    cell.textLabel.font = FONT_MEDIUM(15);
    cell.backgroundColor = UIColor.whiteColor;
    UIView * lineView = [[UIView alloc]init];
    lineView.backgroundColor = self.view.backgroundColor;
    [cell.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(cell.contentView);
        make.height.mas_equalTo(4);
    }];
    return cell;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  54;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * name = [self.jsonArray objectAtIndex:indexPath.item];
    if ([name isEqualToString:@"举报"]) {
        ReportViewController * vc = [[ReportViewController alloc]init];
          vc.userid = self.model.peopleId;
        vc.type = @"0";
        [self.navigationController pushViewController:vc animated:YES];
    } else  if ([name isEqualToString:@"拉黑"])  {
        NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
        [dic setObject:self.model.peopleId forKey:@"pid"];
        [dic setObject:@"1" forKey:@"type"];
        [HttpTool post:Url42 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
            [SQFMDBManager SQ_deletet:self.model.roomId];
            [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"拉黑成功")];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self SQ_Header];
            });
            [self.navigationController popToRootViewControllerAnimated:YES];
        } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
            [YJProgressHUD showMsgWithoutView:httpModel.message];
        }];
    }else  if ([name isEqualToString:@"清空聊天记录"])  {
       BOOL isok = [SQFMDBManager SQ_deletetMessage:self.model.roomId];
        if (isok) {
            self.model.msgType = -1;
            [SQFMDBManager SQ_addListIM:self.model];
            [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"清理完成!")];
            if (self.clenrBlock) {
                self.clenrBlock();
            } 
        } else {
            [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"清理失败,请重试!")];
        }
    }
   
}

-(void)SQ_headerViewBtnClick{
    PeopleDetailViewController * vc = [[PeopleDetailViewController alloc]init];
    vc.uid = self.model.peopleId;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IMPeopleTopView *)topView{
    if (!_topView) {
        _topView = [[IMPeopleTopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 90)];
        [_topView.avatarImageView.btn addTarget:self action:@selector(SQ_headerViewBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _topView;
}
@end
